
/* Copyright 2009 Anthony Surma
This file is part of Java's Kademlia Extension (JKADX).

JKADX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.

JKADX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JKADX.  If not, see <http://www.gnu.org/licenses/>.
 */

package jkadx.core;

import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import jkadx.dht.Contact;
import jkadx.dht.UniqueId;
import jkadx.xml.ElementNode;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;

public class TestCore
{

          static CoreConfig coreSettings;
          static Contact you;
          static ArrayList<ElementNode> mySettings;

    @Test
    public void simple() throws UnknownHostException, InterruptedException
    {

        class TestThread1 implements Runnable
	{
            final String[] s;

            TestThread1(String[] s)
            {
                this.s = s;
            }
	    @Override
	    public void run()
            {
                Core.main(s);
            }
        }

        final int     num         = 8;   // i.e. for open ports 20006 through 20104 use 49
        String[] template = { "-contacts=testCore", "-settings=testCore",
                              "-inport=", "-outport=",
                              "-add=" };

        coreSettings      = new CoreConfig("settings", "contacts", -1, -1);
        mySettings        = coreSettings.getSettings();
        you               = new Contact(new UniqueId(ElementNode.getDataFor("thisNodeId", mySettings)),
                              InetAddress.getByName(ElementNode.getDataFor("externalIpAddress", mySettings)).getAddress(),
                              Integer.parseInt(ElementNode.getDataFor("incomingPort", mySettings)));
        ExecutorService threadPool1 = Executors.newCachedThreadPool();
        
        String[] test = new String[num];     
        int port      = you.port + 2; // start at 2 port numbers after my incoming port
        
        for (int i = 0; i < num; ++i)
            test[i] = template[0] + i + " " + template[1] + i + " " +
                      template[2] + (port++) + " " + template[3] + (port++) + " " +
                      template[4] + you.getInetAddress().getHostAddress() + "/" + you.port + "/" + you.uniqueId.toString();

        //System.out.println("\nInitial Contact: \n");
        //for (String s : test)
        //{
        //    System.out.println(s + "\n");
       // }
        System.out.println();
        for (int i = 0; i < num; i++)
        {
            threadPool1.execute(new TestThread1(test[i].split(" ")));
            Thread.sleep(1000);
        }
        System.out.println(num + " Cores created.");
        threadPool1.shutdown();
        threadPool1.awaitTermination(1, TimeUnit.HOURS);
    }

    public static void main(String[] args)
    {
	org.junit.runner.JUnitCore.main(TestCore.class.getName());
    }
}
